#include <fstream>
#include <iostream>
#include <jsoncpp/json/json.h>

// See install instructions here:
// https://linux.tips/programming/how-to-install-and-use-json-cpp-library-on-ubuntu-linux-os

// Repository on GitHub:
// https://github.com/open-source-parsers/jsoncpp

// Doxygen pages for JsonCpp
// https://open-source-parsers.github.io/jsoncpp-docs/doxygen/index.html

// Run this thing using
// g++ JsonReader.C -o JsonReader -ljsoncpp && ./JsonReader

using namespace Json;
using namespace std;

int main()
{
  // * Attempt to load input file * //
  const char* filename = "tests/data/JsonReader.json";
  ifstream    ifs(filename);
  if(!ifs.good())
  {
    cout << "ERROR: File \"" << filename << "\" does not exist" << endl;
    return 0;
  }

  // * Read as JSON * //
  Reader reader;
  if(!ifs.good())
  {
    cout << "ERROR: File \"" << filename << "\" is not OK" << endl;
    return 0;
  }
  Value root;
  reader.parse(ifs, root);

  // * Load first values * //
  cout << "Last name:  " << root["last name"].asString() << endl;
  cout << "First name: " << root["first name"].asString() << endl;
  cout << "Integer:    " << root["integer"].asInt64() << endl;
  cout << "Float:      " << root["float"].asFloat() << endl;

  // * Load object arrays * //
  const string objname = "batters";
  if(root[objname].isObject())
    for(auto& member : root[objname].getMemberNames())
      cout << member << endl;
  const string arrname = "testArray";
  if(root[arrname].isArray())
    for(int i = 0; i < root[arrname].size(); ++i)
      cout << root[arrname][i] << endl;
  return 1;
}